import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.awt.List;

import ch.quantasy.communication.channel.definition.ChannelManager;
import ch.quantasy.communication.channel.definition.MessageReceiver;
import ch.quantasy.communication.channel.definition.MessageReceiverListener;
import ch.quantasy.communication.channel.implementation.DefaultChannelManager;
import ch.quantasy.communication.channel.implementation.udp.UDPBroadcastChannel;
import ch.quantasy.communication.chat.definition.ChatMessage;


public class DetectorSettingsManager implements MessageReceiverListener<ChatMessage> {

	private ChannelManager<ChatMessage> channelManager;

	private DetectorSettings detSet;
	private DetectorCommands detCmd;
	private List cmdList;

	public DetectorSettingsManager(){
		initSettingsManager();
	}


	@Override
	public void messageReceived(MessageReceiver<ChatMessage> receiver,
			ChatMessage message) {

		String msg = message.getBody().toString();

		System.out.println(msg + " : " + 1);

		if (msg.contains(cmdList.getItem(0))){
			detSet.setEnableMovingSensor(false);
		}
		else if (msg.contains(cmdList.getItem(1))) {
			detSet.setEnableMovingSensor(true);
		}
		else if(msg.contains("getSettings")){

		}

	}

	private void initSettingsManager()
	{
		initNetworkElements();
		this.channelManager.getMessageReceiver().addMessageReceiverListener(this);
		detSet = DetectorSettings.getInstance();
		detCmd = DetectorCommands.getInstance();

		cmdList = detCmd.getCommands();
	}

	private void initNetworkElements() {
		try {
			// http://en.wikipedia.org/wiki/Multicast_address
			// http://code.google.com/p/boxeeremote/wiki/AndroidUDP
			this.channelManager = new DefaultChannelManager<ChatMessage>(
					new UDPBroadcastChannel<ChatMessage>(
							InetAddress.getByName("224.0.2.4"), 4444, 2));
		} catch (final UnknownHostException e) {
			e.printStackTrace();
			// Exception-handling
		} catch (final IOException e) {
			e.printStackTrace();
			// Exception-handling
		}
	}
}
